// pages/schoolinfo/schoolinfo.js
import * as echarts from '../../components/ec-canvas/echarts';
const app = getApp()
const wxRequest = require('../../utils/index')
let timer='';//定时器
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ec: {},
    screenHeight: app.globalData.screenHeight,
    tabs: ['院校概况', '招录数据', '开设专业'],
    years: ['2020', '2021', '2022'],
    yearIndex: 0,
    tabIndex: 0,
    like: false,
    showBoolean: true,
    img_til: '',
    schoolInfo: {},
    schoolDataList: [],
    recruitmentList: [],
    recruitmentData: [],
    option1: {},
    imgIndex: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id == null) {
      wx.showModal({
        title: '提示',
        content: '该院校不存在',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            wx.navigateBack({
              delta: 1
            })
          }
        }
      })
    }
    this.setData({
      schoolId: options.id
    })
    this.getSchoolInfo();
    this.getRecruitmentList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.data.option1 = {
      color: ['#56A1ED', '#EDBC56'],
      tooltip: {
        trigger: 'axis',
        axisPointer: { // 坐标轴指示器，坐标轴触发有效
          type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
        },
        confine: true
      },
      data: ['蒸发量', '降水量'],
      grid: {
        containLabel: true,
        left: 0,
        top: 24,
        right: 20,
        bottom: 0
      },
      xAxis: {
        type: 'category',
        data: ['2019年', '2020年', '2021年'],
        axisTick: {
          show: false
        },
        scale: true,
      },
      yAxis: {
        type: 'value',
        scale: false,
        x: 'center',
        splitLine: {
          lineStyle: {
            type: 'solid',
            color: ['#EEEEEE']
          },
        },
        axisTick: {
          show: false
        },
        axisLine: {
          show: false,
          onZero: false
        }
      },
      series: [{
        data: [120, 200, 100, ],
        type: 'bar',
      }, {
        data: [120, 200, 90, ],
        type: 'bar',
      }]
    };

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
      
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearTimeout(timer)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  clickShow(e) {
    this.setData({
      showBoolean: !this.data.showBoolean
    })
  },
  previewImage(e) {
    let img_til = e.currentTarget.dataset.img_til;
    console.log(img_til);
    wx.previewImage({
      current: img_til, // 当前显示图片的http链接
      urls: [img_til] // 需要预览的图片http链接列表
    })
  },
  clickImg(e) {
    let img_til = e.currentTarget.dataset.img_til;
    console.log(img_til);
    this.setData({
      img_til: img_til
    })
  },
  clickTab(e) {
    this.setData({
      tabIndex: e.currentTarget.dataset.index,
    }, function () {
      if (e.currentTarget.dataset.index == 1) {
        this.getSchoolData();
      }
    })
  },
  clickYear(e) {
    let that = this;
    let yearIndex = e.currentTarget.dataset.index;
    let recruitmentData = this.data.recruitmentList[yearIndex].recruitmentList;
    console.log("recruitmentData", recruitmentData)
    that.setData({
      yearIndex: yearIndex,
      recruitmentData: recruitmentData
    })
  },
  init_bar1() {
    let that = this;
    let ecComponent1 = that.selectComponent('#mychart-dom-bar');

    ecComponent1.init((canvas, width, height) => {
      // 初始化图表
      const chart = echarts.init(canvas, null, {
        width: width,
        height: height
      });
      chart.setOption(this.data.option1);
      wx.hideLoading();
      // 注意这里一定要返回 chart 实例，否则会影响事件处理等
      return chart;
    });
  },
  getSchoolInfo() {
    let that = this;
    let user = app.globalData.user;
    let userId = -1;
    if (user.isSignUp == 1) {
      userId = user.id;
    }
    wxRequest.requestGet('/school/get.do', {
      "id": this.data.schoolId,
      "userId": userId
    }).then(res => {
      if (res.code == '201') {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
        return
      }
      if (res.code == 200) {
        let img_til = "";
        if (res.data.schoolImgList.length > 0) {
          img_til = res.data.schoolImgList[0];
        }
        // console.log('schoolInfo', res.data)
        that.setData({
          schoolInfo: res.data,
          img_til: img_til,
          like: !res.data.isCollection
        },function(){
          timer = setInterval(function () {
            let schoolImgList = that.data.schoolInfo.schoolImgList;
            // if(!schoolImgList.length){
            //   return
            // }
            let imgIndex = that.data.imgIndex;
            if (imgIndex == schoolImgList.length - 1) {
              imgIndex = 0
            } else {
              imgIndex += 1
            }
            that.setData({
              img_til: schoolImgList[imgIndex],
              imgIndex:imgIndex
            })
          }, 4000)
        })
      }
    }).catch(err => {
      console.log(err);
    })
  },
  getSchoolData() {
    let that = this;
    wxRequest.requestGet('school/getData.do', {
      "id": this.data.schoolId
    }).then(res => {
      if (res.code == '201') {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
        return
      }
      if (res.code == 200) {
        let option = that.data.option1;
        let schoolDataList = res.data;
        // console.log(schoolDataList)
        let xData = [];
        let scoreData = [];
        let collectionData = [];
        for (let m in schoolDataList) {
          xData.push(schoolDataList[m].year);
          scoreData.push(schoolDataList[m].outsideCityLine);
          collectionData.push(schoolDataList[m].collectionPoints);
        }
        option.xAxis.data = xData;
        option.series = [{
          data: scoreData,
          type: 'bar',
        }, {
          data: collectionData,
          type: 'bar',
        }]

        that.setData({
          schoolDataList: schoolDataList,
          option1: option
        })
        that.init_bar1();
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: '请求失败',
        icon: 'error'
      })
    })
  },
  getRecruitmentList() {
    let that = this;
    wxRequest.requestGet('school/getRecruitmentList.do', {
      "id": this.data.schoolId
    }).then(res => {
      if (res.code == '201') {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
        return
      }
      if (res.code == 200) {
        // console.error(res.data)
        let recruitmentData = [];
        if (res.data.length > 0) {
          recruitmentData = res.data[0].recruitmentList;
        }
        that.setData({
          recruitmentList: res.data,
          recruitmentData: recruitmentData
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: '请求失败',
        icon: 'error'
      })
    })
  },
  like(e) {
    let that = this;
    let user = app.globalData.user
    if (user.isSignUp == 0) {
      wx.showModal({
        title: '提示',
        content: '请先填写个人信息',
        showCancel: false,
        success(res) {
          if (res.confirm) {}
        }
      })
    } else {
      let data = {
        id: this.data.schoolId,
        type: 0,
        userId: user.id
      };
      if (this.data.like) {
        app.globalData.wxRequest.requestGet('user/collection.do', data).then(res => {
          if (res.code == '201') {
            wx.showToast({
              title: res.msg,
              icon: 'error'
            })
            return
          }
          that.setData({
            like: false
          })
        }).catch(err => {
          wx.showToast({
            title: 请求失败,
            icon: 'error'
          })
        })
      } else {
        app.globalData.wxRequest.requestGet('user/removeCollectionWechat.do', data).then(res => {
          if (res.code == '201') {
            wx.showToast({
              title: res.msg,
              icon: 'error'
            })
            return
          }
          that.setData({
            like: true
          })
        }).catch(err => {
          wx.showToast({
            title: '请求失败',
            icon: 'error'
          })
        })
      }
    }
  },
})